package com.hsteach.web.controller.wage;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.domain.R;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.poi.ExcelUtil;
import com.hsteach.dao.domain.wage.WageRelease;
import com.hsteach.service.wage.WageReleaseService;
import com.hsteach.service.wage.WageTempSnapshotService;
import com.hsteach.web.controller.wage.dto.AddBaseInfoDto;
import com.hsteach.web.controller.wage.dto.HandleRepeatDto;
import com.hsteach.web.controller.wage.dto.UpdateBaseInfoDto;
import com.hsteach.web.controller.wage.dto.WageReleaseListDto;
import com.hsteach.web.controller.wage.vo.WageDataVo;
import com.hsteach.web.controller.wage.vo.WageReleaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工资发布Controller
 *
 * @author fanxquer
 * @date 2023-07-16
 */
@Api(value = "工资发布", tags = "工资发布")
@RestController
@RequestMapping("/web/wage/release")
public class WageReleaseController extends BaseController
{
    @Autowired
    private WageReleaseService wageReleaseService;

    @Autowired
    private WageTempSnapshotService wageTempSnapshotService;

    /**
     * 查询工资发布列表
     */
    @ApiOperation("查询工资发布列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody WageReleaseListDto dto)
    {
        WageRelease query = new WageRelease();
        BeanUtils.copyProperties(dto, query);
        query.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<WageRelease> list = wageReleaseService.selectWageReleaseList(query);
        long total = new PageInfo(list).getTotal();
        List<WageReleaseVo> collect = list.stream().map(e -> {
            WageReleaseVo vo = new WageReleaseVo();
            BeanUtils.copyProperties(e, vo);
            return vo;
        }).collect(Collectors.toList());
        TableDataInfo dataTable = getDataTable(collect);
        dataTable.setTotal(total);
        return dataTable;
    }


    @ApiOperation("上传工资模板及数据校验，code为-2代表有数据未匹配上")
    @PostMapping("/import/{id}")
    public AjaxResult uploadFile(@PathVariable("id") Long id, MultipartFile file) throws Exception {
        return wageReleaseService.importFile(id, file.getInputStream());
    }

    @ApiOperation("处理重名数据")
    @PostMapping("/handleRepeat")
    public AjaxResult handleRepeat(@RequestBody HandleRepeatDto dto) {
        wageReleaseService.handleRepeat(dto);
        return AjaxResult.success();
    }

    /**
     * 获取工资发布信息
     */
    @ApiOperation("获取工资发布信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wageReleaseService.selectWageReleaseById(id));
    }

    /**
     * 获取工资发布上传表格数据
     */
    @ApiOperation("获取工资发布上传表格数据")
    @GetMapping(value = "/data/{id}")
    public R<WageDataVo> getWageData(@PathVariable("id") Long id)
    {
        return R.ok(wageReleaseService.getWageData(id));
    }

    /**
     * 填写基本信息
     */
    @ApiOperation("填写基本信息")
    @PostMapping("/baseInfo")
    public AjaxResult add(@RequestBody AddBaseInfoDto dto)
    {
        Long aLong = wageReleaseService.addBaseInfoDto(dto);
        Map<String, Object> data = Maps.newHashMap();
        data.put("id", aLong);
        AjaxResult success = AjaxResult.success();
        success.put("data", data);
        return success;
    }

    /**
     * 确认发布工资
     */
    @ApiOperation("确认发布工资")
    @PostMapping("/commitRelease/{id}")
    public AjaxResult commitRelease(@PathVariable Long id)
    {
        return toAjax(wageReleaseService.commitRelease(id));
    }

    /**
     * 上传工资明细表完成
     */
    @ApiOperation("上传工资明细表完成（点击下一步）")
    @PostMapping("/importCompleted/{id}")
    public AjaxResult importCompleted(@PathVariable Long id)
    {
        return toAjax(wageReleaseService.importCompleted(id));
    }

    /**
     * 修改基本信息
     */
    @ApiOperation("修改工资发布基本信息")
    @PutMapping("/baseInfo")
    public AjaxResult edit(@RequestBody UpdateBaseInfoDto dto)
    {
        Boolean needReupload = wageReleaseService.updateBaseInfoDto(dto);
        Map<String, Object> data = Maps.newHashMap();
        data.put("needReupload", needReupload);
        return AjaxResult.success(data);
    }

    /**
     * 删除工资发布
     */
    @ApiOperation("删除工资发布")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(wageReleaseService.deleteWageReleaseById(id));
    }

    /**
     * 撤回工资发布
     */
    @ApiOperation("撤回工资发布")
    @PutMapping("/recall/{id}")
    public AjaxResult recall(@PathVariable Long id)
    {
        return toAjax(wageReleaseService.recallById(id));
    }
}
